package servlet;

import entity.User;
import service.Impl.LoginServiceImpl;
import service.LoginService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
    private LoginService loginService = new LoginServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = loginService.login(username, password);
        // 登录检测
        // 如果不为空则创建session对象保存user信息
        if (user != null) {
            HttpSession session = req.getSession();
            session.setAttribute("user", user);
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        } else { // 如果为空则返回登录界面
            resp.sendRedirect("jsp/login.jsp");
        }
    }
}